package com.etv.launcher.ui.component.status;

import android.os.Build;
import android.view.View;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.ConstraintSet;

import com.etv.launcher.R;

import java.util.ArrayList;
import java.util.List;

public class DynamicComponentManager {
    private ConstraintLayout container;
    private View logo;
    private List<View> components = new ArrayList<>();
    private ConstraintSet constraintSet = new ConstraintSet();

    public DynamicComponentManager(ConstraintLayout container) {
        this.container = container;
        this.logo = container.findViewById(R.id.logo);
    }

    // 添加组件
    public void addComponent(View component) {
        if (component.getId() == View.NO_ID) {
            component.setId(View.generateViewId());
        }
        container.addView(component);
        components.add(component);
        updateConstraints();
    }

    // 移除组件
    public void removeComponent(View component) {
        container.removeView(component);
        components.remove(component);
        updateConstraints();
    }

    // 移动组件位置
    public void moveComponent(View component, int newIndex) {
        int oldIndex = components.indexOf(component);
        if (oldIndex != newIndex) {
            components.remove(oldIndex);
            components.add(newIndex, component);
            updateConstraints();
        }
    }

    // 清空所有组件
    public void clearComponents() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            components.forEach(container::removeView);
        }
        components.clear();
        updateConstraints();
    }

    // 更新约束
    private void updateConstraints() {
        constraintSet.clone(container);

        // 连接 Logo 到第一个组件
        if (!components.isEmpty()) {
            constraintSet.connect(logo.getId(), ConstraintSet.RIGHT, components.get(0).getId(), ConstraintSet.LEFT);
        }

        // 连接组件之间
        for (int i = 0; i < components.size() - 1; i++) {
            View current = components.get(i);
            View next = components.get(i + 1);
            constraintSet.connect(current.getId(), ConstraintSet.RIGHT, next.getId(), ConstraintSet.LEFT);
        }

        // 最后一个组件连接到父容器右侧
        if (!components.isEmpty()) {
            constraintSet.connect(components.get(components.size() - 1).getId(), ConstraintSet.RIGHT, ConstraintSet.PARENT_ID, ConstraintSet.RIGHT);
        }

        // 垂直居中
        for (View v : components) {
            constraintSet.connect(v.getId(), ConstraintSet.TOP, ConstraintSet.PARENT_ID, ConstraintSet.TOP);
            constraintSet.connect(v.getId(), ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID, ConstraintSet.BOTTOM);
        }

        // 设置链式约束样式（使用正确的常量名）
        if (!components.isEmpty()) {
            // 链的头部是第一个组件，设置其水平链样式
            constraintSet.setHorizontalChainStyle(components.get(0).getId(), ConstraintSet.CHAIN_SPREAD_INSIDE);
        }

        // 应用约束
        constraintSet.applyTo(container);
    }
}
